package rent.hj.launcher;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v17.leanback.widget.VerticalGridView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import rent.hj.launcher.statusbar.StatusBarPolicy;
import rent.hj.launcher.statusbar.StatusBarView;

public class MainActivity extends Activity {

    private StatusBarPolicy statusBarPolicy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        statusBarPolicy = new StatusBarPolicy(this);
        statusBarPolicy.setCallBack(((StatusBarView) findViewById(R.id.statusbar)).asStatusBar());

        VerticalGridView gridView = (VerticalGridView) findViewById(R.id.grid_view);
        gridView.setVerticalMargin(34);
        gridView.setNumColumns(5);
        gridView.setAdapter(new MyAdapter());

        findViewById(R.id.problem_btn).setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    v.setBackgroundResource(R.drawable.chang_jian_wen_ti);
                } else {
                    v.setBackgroundResource(R.drawable.chang_jian_wen_ti_01);
                }
            }
        });

        findViewById(R.id.back_btn).setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    v.setBackgroundResource(R.drawable.gui_huan);
                } else {
                    v.setBackgroundResource(R.drawable.gui_huan_01);
                }
            }
        });

        findViewById(R.id.problem_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,ProblemActivity.class));
            }
        });

        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (service1 != null) {
                    service1.showReturnDialog();
                }
            }
        });

        bindService(new Intent(this, LockService.class), connection, Context.BIND_AUTO_CREATE);

        final TextView title_txt = (TextView) findViewById(R.id.title);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(LockService.LOCK_COUNTDOWN_TIME_ACTION);
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                long time = intent.getLongExtra(LockService.TIME_EXTRA, -1);
                if (time != -1) {
                    title_txt.setText(showTime(time));
                }
            }
        }, intentFilter);
    }

    LockService service1;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            service1 = ((LockService.MyBinder) service).getService();
            service1.showLockDialog();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private String showTime(long time) {
        int hour = 60 * 60;
        int hours = (int) (time / hour);
        int min = (int) (time - hours * hour) / 60;

        if (hours != 0) {
            return "剩余使用时长" + hours + "小时" + min + "分钟";
        } else if (min != 0){
            return "剩余使用时长" + min + "分钟";
        } else if (time != 0){
            return "剩余使用时长不足1分钟";
        } else {
            return "剩余使用时长0分钟";
        }
    }

    private class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = View.inflate(MainActivity.this, R.layout.film_item_layout, null);

            view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        v.animate().scaleX(1.2f).scaleY(1.2f).start();
                        v.setElevation(10);
                    } else {
                        v.animate().scaleX(1f).scaleY(1f).start();
                        v.setElevation(0);
                    }
                }
            });

            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {


        }

        @Override
        public int getItemCount() {
            return 100;
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        statusBarPolicy.onDestory();
    }
}
